import * as Request from "@/utils/Request";
// eslint-disable-next-line no-unused-vars
import { getToken, setToken, removeToken } from "@/utils/TokenUtil";
// eslint-disable-next-line no-unused-vars
import router from "@/router";
const userInfoUrl = "/admin_server/auth/user/getUserInfo";
const loginUrl = "/admin_server/oauth/login";
const logoutUrl = "/admin_server/auth/user/logout";
const user = {
  state: {
    token: "", // token
    roles: [], // 用户角色接口原始数据
    permissions: [], // 菜单资源接口原始数据
    info: {}, // 用户信息
  },
  mutations: {
    /**
     * 设置用户信息
     * @param {} state
     * @param {*} data
     */
    SET_USER_INFO(state, data) {
      state.info = data;
    },
    /**
     * 设置用户角色
     * @param {} state
     * @param {*} data
     */
    SET_USER_ROLES(state, data) {
      state.roles = data;
    },
    /**
     * 设置用户资源
     * @param {} state
     * @param {*} data
     */
    SET_USER_PERMISSIONS(state, data) {
      state.permissions = data;
    },
    /**
     * 设置token
     * @param {} state
     * @param {*} data
     */
    SET_TOKEN(state, data) {
      state.token = data;
    },
  },
  actions: {
    /**
     * 登录
     * @param {} param0
     * @param {*} data
     * @returns
     */
    LOGIN({ commit }, data) {
      return new Promise((resolve, reject) => {
        Request.loginRequest(loginUrl, data)
          .then((login) => {
            if (login && login.code === 200) {
              const token = login.result.access_token;
              commit("SET_TOKEN", token);
              setToken(token);
              resolve(login);
            } else {
              reject(login);
            }
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    /**
     * 获取用户信息
     * @param {*} param0
     * @returns
     */
    GET_USER_INFO({ commit }) {
      return new Promise((resolve, reject) => {
        Request.getRequest1(userInfoUrl)
          .then((res) => {
            if (res.success) {
              commit("SET_USER_INFO", res.result);
              commit("SET_USER_ROLES", res.result.roleDtoList);
              commit("SET_USER_PERMISSIONS", res.result.permissionDtoList);
              resolve(res.result);
            }
          })
          .catch(() => {
            reject();
          });
      });
    },
    /**
     * 登出
     * @param {*} param0
     */
    // eslint-disable-next-line no-unused-vars
    LOGOUT({ commit }) {
      return new Promise((resolve, reject) => {
        Request.getRequest1(logoutUrl)
          .then((res) => {
            resolve(res);
            removeToken();
            router.push({ name: "Login" });
          })
          .catch((err) => {
            reject(err);
            removeToken();
            router.push({ name: "Login" });
          })
          .finally(() => {
            // removeToken()
            // commit('SET_TOKEN', '')
            // commit('SET_USER_INFO', {})
            // commit('SET_USER_ROLES', [])
            // commit('SET_USER_PERMISSIONS', [])
            location.reload();
          });
      });
    },
  },
};

export default user;
